home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 478 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  75 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: jbm@jbm.nada.kth.se (Jonas Mvlsd)
  3. Newsgroups: comp.std.c++
  4. Subject: Calling X(int) from X()'s init list
  5. Date: 25 Feb 1996 00:07:28 GMT
  6. Organization: Royal Institute of Technology, Stockholm, Sweden
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Distribution: world
  9. Message-ID: <4gnush$phg@news.kth.se>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Mime-Version: 1.0
  12. Content-Type: text/plain; charset=iso-8859-1
  13. Content-Transfer-Encoding: quoted-printable
  14. X-Nntp-Posting-Host: jbm.nada.kth.se
  15. Originator: clamage@taumet
  16.  
  17. Why is it not allowed to delegate the initialisation to another=20
  18. constructor, if that constructor is *the only thing* in the=20
  19. member initialisation list?=20
  20.  
  21. It seems to me that in many cases, one could avoid some code=20
  22. duplication and gain some clarity, at no conceptual or runtime cost.
  23. The increased clarity would come from folding different constructor
  24. cases into one case by calling the most general constructor in
  25. the member initialisation list of the other constructors.
  26. (I know this is not always clearer, but sometimes it is.)
  27.  
  28. Is it a case of: "Yes, it is basically a good idea, but it is too=20
  29. late in the standardisation process. Anyway, we have to draw the
  30. line somewhere."
  31.  
  32. Or is it more like: "Of course you should not do it that way, look here..=
  33. ."
  34.  
  35.  
  36. A small example to illustrate the technical (not the conceptual)
  37. detatils of my question:
  38.  
  39. #include <iostream.h>
  40.  
  41. class X
  42. {
  43. public:
  44.     X (int i) : i_(i) {}
  45.  
  46.     X () : X(34) {}        // Not allowed
  47.  
  48.     int i() {return i_;}
  49.  
  50. private:
  51.     int i_;
  52. };
  53.  
  54.  
  55.  
  56. int
  57. main ()
  58. {
  59.     X a(55);
  60.     X b;
  61.  
  62.     cout << a.i() << ' ' << b.i() << endl;
  63. }
  64. =09
  65.  
  66. ---
  67. Jonas M=F6ls=E4, jbm@nada.kth.se
  68.  
  69. [ To submit articles: Try just posting with your newsreader.
  70.               If that fails, use mailto:std-c++@ncar.ucar.edu
  71.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  72.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  73.   Comments? mailto:std-c++-request@ncar.ucar.edu
  74. ]
  75.